package cfig.toolset

import org.apache.commons.exec.CommandLine
import org.apache.commons.exec.DefaultExecutor
import org.slf4j.LoggerFactory
import se.vidstige.jadb.JadbDevice

class AdbTool {
    private val log = LoggerFactory.getLogger(AdbTool::class.java)

    fun shellCheckCall(dut: JadbDevice, cmd: String): Boolean {
        return checkCall(dut, "shell $cmd")
    }

    fun checkCall(dut: JadbDevice, rawCmd: String): Boolean {
        var ret = true
        val cmdLine = CommandLine.parse("adb -s ${dut.serial} $rawCmd")
        log.info(cmdLine.toString())
        try {
            DefaultExecutor().execute(cmdLine)
        } catch (e: Exception) {
            log.warn("$e")
            ret = false
        }
        return ret
    }

    @ExperimentalStdlibApi
    fun getProp(dut: JadbDevice, k: String): String {
        return dut.executeShell("getprop $k").readBytes().decodeToString().trim()
    }
}
